import loadData from '../services/categoryService';

export default {

  namespace: 'category',

  state: {
    contentData:{},
    showContent:false,
    showLoading:false,
    showEmpty:false,
    showError:false,
    showFail:false,
    refreshing:false,
    emptyMsg:'',
    errorMsg:'',

    selectedIndex:0,
    indexList:[],
    categoryList:[],
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
      dispatch({
        type:'firstLoadData'
      });
    },
  },

  effects: {
    *firstLoadData({ payload }, { call, put }) {  // eslint-disable-line
      yield put({
        type: 'onFirstLoadData'
      });
      const result = yield call(loadData, {});
      yield put({
        type: 'onLoadSuccess',
        payload: { ...result }
      });
    },
  },

  reducers: {

    onFirstLoadData(state, action) {
      return {
        ...state,
        showLoading:true,
        showContent:false,
        showEmpty:false,
        showError:false,
        showFail:false,
        showRefreshing:false,
      };
    },

    onLoadSuccess(state, { payload }) {
      if (payload.head.code === '0') {

        let brandList = payload.body.brandData;
        let brandDict = {};
        brandDict.rootClassifyName = '品牌';
        brandDict.subClassifyList = [];

        for (let j=0; j<brandList.length; j++) {
          let subDict = brandList[j];
          let resultDict = {};
          resultDict.isBrand = true;
          resultDict.subClassifyName = subDict.brandName;
          resultDict.subIcon = subDict.logo;
          resultDict.subClassifyId = subDict.id;
          if (! resultDict.subIcon || resultDict.subIcon === undefined || resultDict.subIcon === 'http://file.zfenb.com') {
            continue;
          }
          brandDict.subClassifyList.push(resultDict);
        }
        brandDict.selected = (state.selectedIndex === 0);

        let indexList = [];
        indexList.push(brandDict);
        let categoryList = payload.body.classifyData;
        for (let i=0; i<categoryList.length; i++) {
          let rootDict = categoryList[i];
          rootDict.selected = (state.selectedIndex === i+1);
          indexList.push(rootDict);
        }

        if (indexList.length < state.selectedIndex-1) {
          state.selectedIndex = 0;
        }
        let dict = indexList[state.selectedIndex];
        categoryList = dict.subClassifyList;
        return {...state,
          contentData:payload,
          showContent:true,
          showLoading:false,
          showEmpty:false,
          showError:false,
          showFail:false,
          refreshing:false,

          indexList: indexList,
          categoryList: categoryList,
        };
      }
      else {
        // 服务器问题
        return {...state,
          contentData:payload,
          showLoading:false,
          showContent:false,
          showEmpty:false,
          showFail:false,
          showError:true,
          refreshing:false,
          errorMsg:payload.head.message,
        };
      }
    },

    changeIndex(state, {payload}) {

      console.log(payload, typeof(payload));
      for (let i=0; i<state.indexList.length; i++ ) {
        let data = state.indexList[i];
        data.selected = false;
        if (payload === i) {
          data.selected = true;
          // data = {...data, selected: true};
          state.categoryList = data.subClassifyList;
        }
      }

      return {
        ...state,
        selectedIndex:payload,
      }
    }

  },

};
